package com.monitor.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.service.EquipmentService;
import com.config.service.LineConfigService;
import com.monitor.service.MonitorService;

@Controller
@RequestMapping("/monitor")
public class MonitorController{
	@Resource(name="lineConfigService")
	private LineConfigService lineService;
	@Resource(name="equipmentService")
	private EquipmentService equipmentService;
	@Resource(name="monitorService")
	private MonitorService  monitorService;
	
	/**
	 * 单机实时监控页面
	 * @return
	 */
	@RequestMapping("/single")
	public String singleRealtime(Model model){
		model.addAttribute("currentName","单机实时监控");
		List<Equipment> list=equipmentService.listByLine(null);
		Map<String,String> map = new HashMap<String,String>();
		for(Equipment e:list){
			map.put(e.getId()+"*"+e.getLineId(), e.getEquNo());
		}
		model.addAttribute("equs",map);
		return "monitor/single";
	}
		
	/**
	 * 动态刷新单机实时监控页面
	 * @param lineId
	 * @param equId
	 * @param model
	 * @return
	 */
	@RequestMapping("/singlerefresh")
	public Map<String, Object> singleRefresh(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				monitorService.getSingleMonitorData(line.getId(), equ.getId(), model);
			}
		}else{
			//根据前台所选的生产线和设备刷新
			monitorService.getSingleMonitorData(lineId, equId, model);
		}
		return model;
	}
	
	/**
	 * 整线实时监控页面
	 * @return
	 */
	@RequestMapping("/line")
	public String lineRealtime(Model model){
		//获取第一条生产线
		LineConfig line=lineService.findFirstLine();
		List<Equipment> equipList = equipmentService.listByLine(line.getId());
		List<String> items = new ArrayList<String>();
		for(int i=0;i<equipList.size();i++){
			items.add("items"+i);
		}
		model.addAttribute("items", items);
		model.addAttribute("currentName","整线实时监控");
		return "monitor/line";
	}
	
	/**
	 * 动态刷新单机实时监控页面
	 * @param lineId
	 * @param equId
	 * @param model
	 * @return
	 */
	@RequestMapping("/linerefresh")
	@ResponseBody
	public Map<String, Object> linerefresh(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				  monitorService.getlineMonitorData(line.getId(), model);
			}
		}else{
			//根据前台所选的生产线刷新
			monitorService.getlineMonitorData(lineId, model);
		}
		return model;
	}
}
